package cn.wolfcode.luowowo2.website.web.controller;

import cn.wolfcode.luowowo2.article.domain.*;
import cn.wolfcode.luowowo2.article.service.*;
import cn.wolfcode.luowowo2.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo2.common.query.QueryObject;
import cn.wolfcode.luowowo2.common.query.TravelQueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by Albert on 2019/7/9.
 */
@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String getHotRegion(Model model){
        List<Region> list = regionService.getHotRegion();
        model.addAttribute("hotRegions",list);
        return "destination/index";
    }

    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model){
        Long[] refIds =null;
        if (regionId==-1L) {
            refIds = destinationService.getChildIdsByParentId(1L);
        }else{
            Region region = regionService.get(regionId);
            refIds = region.getRefIds();
        }

        List<Destination> list = destinationService.getHotDestByRegionId(refIds);
        List<Destination> leftList = list.subList(0, list.size() / 2 + 1);
        model.addAttribute("leftDests",leftList);
        List<Destination> rightList = list.subList(list.size() / 2 + 1,list.size());
        model.addAttribute("rightDests",rightList);
        return "destination/hotdestTpl";
    }

    @RequestMapping("detail")
    public String getDetail(Model model,Long id,@ModelAttribute("qo") DestinationQueryObject qo){

        List<Destination> toastList = destinationService.getToastList(id);
        model.addAttribute("toasts",toastList);
        Destination destination = destinationService.get(id);
        model.addAttribute("dest",destination);
        return "destination/survey";
    }

    @RequestMapping("guide")
    public String guide(Model model,Long id){
        List<Destination> toastList = destinationService.getToastList(id);
        model.addAttribute("toasts",toastList);
        Destination destination = destinationService.get(id);
        model.addAttribute("dest",destination);

        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByStrategyId(1L);
        model.addAttribute("catalogs",catalogs);

        List<StrategyDetail> strategyDetails = strategyDetailService.getStrategyByDestId(id);
        model.addAttribute("strategyDetails",strategyDetails);

        return "destination/guide";
    }

    @RequestMapping("travels")
    public String travels(Model model,@ModelAttribute("qo") TravelQueryObject qo){
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }


}
